package eng.jTrussSolver.Material;

import java.util.HashMap;
import java.util.Map;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class MaterialsHashMap extends HashMap<String, MaterialData>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public MaterialsHashMap()
	{
	}




	public void addMaterial(MaterialData materialData) {
		this.put(materialData.Name, materialData);
	}

	public int getMaterialsCount() {
		return this.size();
	}

	/**
	 * removes the material 
	 * @param aa
	 * TODO: to change for HashMap compatibility
	 */
	public void removeMaterial(String MaterialName) {
		this.remove(MaterialName);

	}

	/**
	 * Sets material to index 
	 * @param currentSelectedMaterial
	 * @param b
	 */
	public void setMaterial(String oldname, MaterialData b) {
		this.remove(oldname);
		this.put(b.Name,b);
	}


	/**
	 * Code For Materials Data
	 * @param doc
	 * @return
	 */
	public Element prepareXMLElement(Document doc) {
		
		Element M = doc.createElement("Materials");


		for (Map.Entry<String, MaterialData> entr: this.entrySet())
		{

			Element materialElement = doc.createElement("Material");
			M.appendChild(materialElement);

			Element el = doc.createElement("Name");
			el.appendChild(doc.createTextNode(entr.getValue().Name));
			materialElement.appendChild(el);

			Element c = doc.createElement("E");
			c.appendChild(doc.createTextNode(String.valueOf(entr.getValue().E)));
			materialElement.appendChild(c);

			Element sep = doc.createElement("S_ep");
			sep.appendChild(doc.createTextNode(String.valueOf(entr.getValue().s_ep)));
			materialElement.appendChild(sep);
		}
		return M;
	}
	
	/**
	 * Clears() Hashmap and also resets counter.
	 */
	public void clear()
	{
		super.clear();
	}
}
